#include "docwidget.h"
#include "QDebug"
#include <QPixmap>

/*
 显示pdf的大窗口
*/

DocWidget::DocWidget(PDFKit *p, QWidget *parent) : pdf(p), QWidget(parent)
{    
    scrollArea = new QScrollArea(this);

    docView = new DocView(pdf, this);
setObjectName("DocWidget");

    scrollArea->setWidget(docView);
    scrollArea->setAlignment(Qt::AlignCenter);

    scrollbar = scrollArea->verticalScrollBar();
    connect(scrollbar, &QScrollBar::valueChanged, docView, &DocView::valueChange);

    mainLayout = new QVBoxLayout();
    mainLayout->addWidget(scrollArea);
    mainLayout->setContentsMargins(1,1,1,1);
    setLayout(mainLayout);
}

DocWidget::~DocWidget()
{

}

void DocWidget::gotoPage(int page)
{
    unsigned long value = page * pdf->get_pageHeight();
    scrollbar->setValue(value);
}

int DocWidget::getCurrentPage()
{
    return docView->getCurrentPage();
}

void DocWidget::changeImageSize(int flag)
{
    docView->changeImageSize(flag);
}

void DocWidget::resizeEvent(QResizeEvent *)
{
    _resize();
}

void DocWidget::refreshWidget()
{
    update();
    _resize();

}

void DocWidget::_resize()
{
    if(pdf->get_pageWidth() > scrollArea->size().width())
    {        
        docView->resize(pdf->get_pageWidth()+scrollArea->size().width(), pdf->get_totalPage()*(pdf->get_pageHeight()+10));
    }
    else
    {
        docView->resize(scrollArea->size().width()-scrollbar->size().width(), pdf->get_totalPage()*(pdf->get_pageHeight()+10));
    }

}


